package Leetcode;

import java.util.HashSet;
import java.util.Set;

public class problem8 {
    public static int myAtoi(String str) {
        str = str.trim();
        if(str.length() == 0){
            return 0;
        }

        int result = 0;
        boolean flag = true;

        for(int i = 0;i < str.length();i++){
            char c = str.charAt(i);

            if(i == 0 && (c == '+' || c == '-')){
                flag = c == '+' ? true : false;
            }else if(c >= '0' && c <= '9'){
                if(result > ((Integer.MAX_VALUE - (c - '0')) / 10)){
                    return flag ? Integer.MAX_VALUE : Integer.MIN_VALUE;
                }
                result = result * 10 + (c - '0');
            }else{
                return flag ? result : -result;
            }
        }
        return flag ? result : -result;
    }

    public static void main(String[] args) {
        System.out.println(myAtoi("  0000000000012345678"));
    }
}
